<template>
	<div>
		<el-breadcrumb separator-class="el-icon-arrow-right">
		   <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
		   <el-breadcrumb-item>商品管理</el-breadcrumb-item>
		   <el-breadcrumb-item>商品分类</el-breadcrumb-item>
		</el-breadcrumb>
		<el-card>
			<!-- 添加按钮 -->
			<el-row>
				<el-col>
					<el-button @click="showAddcateDialog" type="primary">添加商品</el-button>
				</el-col>
			</el-row>
			<!-- 表格 -->
			<tree-table class="treetable" :show-row-hover='false' border index-text='#' show-index :expand-type='false' :selection-type='false' :data='catelist' :columns='columns'>
				<!-- 是否有效 -->
				<template slot='isok' slot-scope='scope'>
					<i style="color: lightgreen;" v-if="scope.row.cat_deleted=== false" class="el-icon-success"></i>
					<i style="color: red;" v-else class="el-icon-error"></i>
				</template>
				<!-- 排序 -->
				<template slot='order'  slot-scope='scope'>
						<el-tag v-if="scope.row.cat_level === 0" size="mini">一级</el-tag>
						<el-tag v-else-if="scope.row.cat_level === 1" size="mini" type="success">二级</el-tag>
						<el-tag v-else size="mini" type="warning">三级</el-tag>
				</template>
				<!-- 操作 -->
				<template slot='opt'  slot-scope='scope' >
					<el-button  size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
					<el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
				</template>
			</tree-table>
			<!-- 分页区域 -->
			 <el-pagination
			      @size-change="handleSizeChange"
			      @current-change="handleCurrentChange"
			      :current-page="querInfo.pagenum"
			      :page-sizes="[3, 5, 10, 15]"
			      :page-size="querInfo.pagesize"
			      layout="total, sizes, prev, pager, next, jumper"
			      :total="total">
			    </el-pagination>
		</el-card>
		<!-- 添加分类对话框 -->
		<el-dialog
			@close="addCateDiaClosed()"
		  title="添加分类"
		  :visible.sync="addCateDialogVisible"
		  width="50%">
		  <!-- 添加分类表单 -->
			<el-form :model="addCtaeForm" :rules="addCtaeFormRules" ref="addCtaeFormRef" label-width="100px" class="demo-ruleForm">
				<el-form-item label="分类名称:" prop="cat_name">
					<el-input v-model="addCtaeForm.cat_name"></el-input>
				</el-form-item>
				<el-form-item label="父级分类:">
					 <el-cascader
						expand-trigger="hover"
					    v-model="seletedKeys"
					    :options="parentCateList"
					    :props="cascaderProps"
					    @change="parentCateChanged()"></el-cascader>
				</el-form-item>
			</el-form>
		  <span slot="footer" class="dialog-footer">
		    <el-button @click="addCateDialogVisible = false">取 消</el-button>
		    <el-button type="primary" @click="addCate">确 定</el-button>
		  </span>
		</el-dialog>
	</div>
</template>

<script>
export default {
  data () {
    return {
      // 选中的父级分类数组
      seletedKeys: [],
      // 指定级联选择器
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      // 父级分类列表
      parentCateList: [],
      addCtaeForm: {
        cat_name: '',
        cat_pid: 0,	// 父级分类的id
        cat_level: 0 	// 分类等级
      },
      addCtaeFormRules: {
        cat_name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }
        ]
      },
      addCateDialogVisible: false,
      querInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      catelist: [],
      total: 0,
      columns: [{
        label: '分类名称',
        prop: 'cat_name'
      }, {
        label: '是否有效',
        type: 'template',
        template: 'isok'
      }, {
        label: '排序',
        type: 'template',
        template: 'order'
      }, {
        label: '操作',
        type: 'template',
        template: 'opt'
      }
      ]
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    async getCateList () {
      const { data: res } = await this.$http.get('categories', { params: this.querInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类失败')
      }
      this.catelist = res.data.result
      this.total = res.data.total
    },
    handleSizeChange (newSize) {
      this.querInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange (newPage) {
      this.querInfo.pagenum = newPage
      this.getCateList()
    },
    // 添加分类事件
    showAddcateDialog () {
      // 获取父级分类的数据列表
      this.getParentCateList()
      this.addCateDialogVisible = true
    },
    // 获取父级分类的数据列表
    async getParentCateList () {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) {
        this.$message.error('获取父级分类数据失败')
      }
      this.parentCateList = res.data
    },
    parentCateChanged () {
      // console.log(tihs.seletedKeys)
      // 如果 数组中的length大于0 就选中了了父级分类
      if (this.seletedKeys.length > 0) {
        this.addCtaeForm.cat_pid = this.seletedKeys[this.seletedKeys.length - 1]
        this.addCtaeForm.cat_level = this.seletedKeys.length
      } else {
        this.addCtaeForm.cat_pid = 0
        this.addCtaeForm.cat_level = 0
      }
    },
    addCate () {
      // 添加新的分类
      this.$refs.addCtaeFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.addCtaeForm)
        if (res.meta.status !== 201) {
          this.$message.error('添加分类失败')
        }
        this.$message.success('添加分类成功')
        this.getCateList()
        this.addCateDialogVisible = false
      })
    },
    addCateDiaClosed () {
      this.$refs.addCtaeFormRef.resetFields()
      this.seletedKeys = []
      this.addCtaeForm.cat_pid = 0
      this.addCtaeForm.cat_level = 0
    }
  }
}
</script>

<style scoped>
	.treetable{
		margin-top: 15px;
	}
</style>
